//
//  ActivityTableViewCell.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/15.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "ActivityTableViewCell.h"

@implementation ActivityTableViewCell {
    //ActivityModel * activityModel;
    UIImageView *goodsImageView;
    UIImageView *dottedLine;
    UILabel *priceLabel;
    UILabel *title;
    
    UIView *notStartView;
    UIView *timeArea;
    UILabel *timeLabel;
    UILabel *number;
    UILabel *hourLabel ;
    UILabel * _minuteLabel;
    UILabel *_secondLabel ;
    
    UIView *startingView;
    UILabel *startingGoodsNumberLabel;
    UILabel *startingTimeLabel;
    
    
    UIView *endedView;
    UILabel *endedViewTimeLabel;
    UIButton *endedButton;
    UILabel *endedGoodsNumberLabel;
    UILabel *numberLabel;

    
    ActivityModel *activityModel;
    
}

- (void)setTltleIsReadColor{
    
     self->title.textColor = [UIColor grayColor];
    
}


- (void) refreshTime {
    
    if (self->activityModel.status == 3) {
        return;
    }
    
    int64_t currentTimestamp = ++self->activityModel.current_at;
    
    if (self->activityModel.status == 1) {
        
        if (currentTimestamp >= self->activityModel.start_at && currentTimestamp <= self->activityModel.end_at) {
            self->activityModel.status = 2;
            
            [self setActivityModel:self->activityModel];
            
            return;
        }
        
        
        int64_t timeDifference = self->activityModel.start_at - currentTimestamp;
        
        //int days = ((int)timeDifference)/(3600*24);
        int hours = ((int)timeDifference) % 86400 / 3600;
        int minutes = ((int)timeDifference) % 86400 % 3600 / 60;
        int seconds = ((int)timeDifference)% 86400 % 3600 % 60 % 60;
        
        self->hourLabel.text = [NSString stringWithFormat:@"%.2d", hours];
        self->_minuteLabel.text = [NSString stringWithFormat:@"%.2d", minutes];
        self->_secondLabel.text = [NSString stringWithFormat:@"%.2d", seconds];
    }
    
    if (self->activityModel.status == 2 && currentTimestamp > self->activityModel.end_at) {
        self->activityModel.status = 3;
        
    }
    
}

//-(void)setActivity:(NSDictionary *)activity {
//    
//    //商品图片
//    [goodsImageView sd_setImageWithURL:[NSURL URLWithString:activity[@"img_url"]]
//                      placeholderImage:nil];
//}


-(void)setActivityModel:(ActivityModel *)activityModel {
    
    self->activityModel = activityModel;
    
   //商品图片
    [goodsImageView sd_setImageWithURL:[NSURL URLWithString:activityModel.img_url]
                  placeholderImage:nil];
    
    
   
    NSString *key = [NSString stringWithFormat:@"is_read_activity_id_%d", activityModel.id ];
        
    NSString *isRead =  [[PINCache sharedCache] objectForKey:key];
    
    if (isRead != nil) {
        self->title.textColor = [UIColor grayColor];
    } else {
        self->title.textColor = [UIColor blackColor];
    }
    
    self->title.text  = activityModel.title;
    
    //物品数量
    self->number.text = [NSString stringWithFormat:@"%d /件", activityModel.number];
    self->startingGoodsNumberLabel.text =[NSString stringWithFormat:@"%d /件", activityModel.number];
    self->endedGoodsNumberLabel.text =[NSString stringWithFormat:@"%d /件", activityModel.number];
    
    NSDateFormatter *dateFormatter = NSDateFormatter.new;
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *startTime =[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:activityModel.start_at]];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%d", activityModel.goods.price] attributes:attribtDic];
    self->priceLabel.attributedText = attribtStr;
    
  
    
    if(activityModel.status == 1){
        
        notStartView.hidden = NO;
        startingView.hidden = YES ;
        endedView.hidden = YES;
        
        timeLabel.text = [NSString stringWithFormat:@"%@开抢", startTime];
        
        
        }else if (activityModel.status == 2){
            
            notStartView.hidden = YES;
            startingView.hidden = NO ;
            endedView.hidden = YES;
            
            startingTimeLabel.text =[NSString stringWithFormat:@"%@ 正在进行", startTime];
        
        }else if (activityModel.status == 3){
        
            notStartView.hidden = YES;
            startingView.hidden = YES ;
            endedView.hidden = NO;
        
            endedViewTimeLabel.text = [NSString stringWithFormat:@"%@",startTime];
            
        }
    
    
}

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        UIView* dividerView =UIView.new;
        dividerView.backgroundColor =UIColorFromRGB(0xEDEEEF);
        [self addSubview:dividerView];
        
        [dividerView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.top).offset(0);
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.height.equalTo(7);
        }];
        
        
        //商品图片
        goodsImageView = UIImageView.new;
        goodsImageView.backgroundColor =[UIColor redColor];
        [self addSubview:goodsImageView];
        
        [goodsImageView makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(MAIN_SCREEN_WIDTH/2);
            make.top.equalTo(self.top).offset(7);
            make.width.equalTo(MAIN_SCREEN_WIDTH);
        }];
        
        
        //分割线
        UIImageView* dottedLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dotted_line"]];
        dottedLine.contentMode = UIViewContentModeScaleAspectFill;
        dottedLine.clipsToBounds = YES;
        [self addSubview:dottedLine];
        
        [dottedLine makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottom).offset(-40);
            make.left.equalTo(self.left).offset(8);
            make.right.equalTo(self.right).offset(-8);
            make.height.equalTo(0.5);
        }];
        
        
        priceLabel =UILabel.new;
        priceLabel.backgroundColor =UIColorFromRGB(0xFFFFFF);
       
        [priceLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        [self addSubview:priceLabel];
        
        [self addSubview:priceLabel];
        [priceLabel makeConstraints:^(MASConstraintMaker *make) {
            //  make.top.equalTo(self.view.top).offset(30);
            make.right.equalTo(self.right).offset(-5);
            make.top.equalTo(self->goodsImageView.bottom).offset(10);
            
        }];
        

        title = UILabel.new;
        //title.backgroundColor = UIColorFromRGB(0xFFFFFF);
        title.textColor = [UIColor blackColor];
       
        [self addSubview:title];
        
        [title makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self->priceLabel.left).offset(-10);
            make.top.equalTo(self->goodsImageView.bottom).offset(10);
            make.left.equalTo(self.left).offset(10);
        }];
        
        
        /*******未开始页面******/
        notStartView = UIView.new;
        notStartView.hidden = YES;
        [self addSubview:notStartView];
        
        [notStartView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(dottedLine.bottom).offset(8);
            make.left.equalTo(self.left).offset(8);
            make.right.equalTo(self.right).offset(-8);
            make.height.equalTo(24);
        }];
        
        
        timeArea = UIView.new;
        timeArea.layer.cornerRadius = 12;
        timeArea.backgroundColor = UIColorFromRGB(0xE75650);
        [notStartView addSubview:timeArea];
        
        UIImageView *timeIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_time"]];
      
        [timeArea addSubview:timeIcon];
        
        [timeIcon makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self->timeArea.left).offset(8);
            make.centerY.equalTo(self->timeArea.centerY);
            make.width.equalTo(11);
            make.height.equalTo(11);
        }];
        
        
        timeLabel = UILabel. new;
        timeLabel.textColor = [UIColor whiteColor];
        //显示行数
        timeLabel.numberOfLines = 1;
        timeLabel.font=[UIFont systemFontOfSize:14];
        
        [timeArea addSubview:timeLabel];
        
        [timeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(timeIcon.right).offset(3);
            make.centerY.equalTo(self->timeArea.centerY);
        }];
        
        [timeArea makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->notStartView).offset(0);
            make.left.equalTo(self->notStartView.left).offset(0);
            make.right.equalTo(self->timeLabel.right).offset(8);
            make.height.equalTo(24);
        }];
        
        
        //倒计时页面
        //小时框
        UIView *hourView = UIView.new;
        hourView.layer.borderWidth=1/[[UIScreen mainScreen]scale];
        hourView.layer.borderColor = [UIColorFromRGB(0xD4D4D4) CGColor];
        [timeArea addSubview:hourView];
        
        [hourView makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self->timeArea.centerY);
            make.left.equalTo(self->timeArea.right).offset(6);
            make.width.equalTo(21);
            make.height.equalTo(20);
            }];
        
        
        hourLabel =UILabel.new;
        hourLabel.textColor =[UIColor blackColor];
        hourLabel.font=[UIFont systemFontOfSize:15];
        [hourView addSubview:hourLabel];
        
        [hourLabel makeConstraints:^(MASConstraintMaker *make) {
              make.center.equalTo(hourView.center);
        }];
        
        
        //冒号
        UILabel *colon1Label = UILabel.new;
        colon1Label.font = [UIFont systemFontOfSize:14];
        colon1Label.textColor = [UIColor blackColor];
        colon1Label.text = @":";
        [notStartView addSubview:colon1Label];
        
        [colon1Label makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(hourView.centerY);
            make.left.equalTo(hourView.right).offset(4);
        }];
        
        
        //分钟的框
        UIView *minuteView = UIView.new;
        minuteView.layer.borderWidth = 1/[[UIScreen mainScreen] scale];
        minuteView.layer.borderColor = [UIColorFromRGB(0xD4D4D4) CGColor];
        [notStartView addSubview:minuteView];
        
        [minuteView makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(hourView.centerY);
            make.left.equalTo(colon1Label.right).offset(4);
            make.width.equalTo(21);
            make.height.equalTo(20);
        }];
        
        
        _minuteLabel = UILabel.new;
        _minuteLabel.font = [UIFont systemFontOfSize:15];
        _minuteLabel.textColor = [UIColor blackColor];
      
        [minuteView addSubview:_minuteLabel];
        
        [_minuteLabel makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(minuteView.center);
        }];
        
        
        // 冒号2 colon
        UILabel *colon2Label = UILabel.new;
        //colon2Label.font = [UIFont fontWithName:MAIN_FONT_NAME size:14];
        colon2Label.textColor = [UIColor blackColor];
        colon2Label.text = @":";
        [notStartView addSubview:colon2Label];
        
        [colon2Label makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(minuteView.centerY);
            make.left.equalTo(minuteView.right).offset(4);
        }];
        
        
        // 秒
        UIView *secondView = UIView.new;
        secondView.layer.borderWidth = 1/[[UIScreen mainScreen] scale];
        secondView.layer.borderColor = [UIColorFromRGB(0xD4D4D4) CGColor];
        [notStartView addSubview:secondView];
        
        [secondView makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(minuteView.centerY);
            make.left.equalTo(colon2Label.right).offset(4);
            make.width.equalTo(21);
            make.height.equalTo(20);
        }];
        
        
        _secondLabel = UILabel.new;
        _secondLabel.font = [UIFont systemFontOfSize:15];
        _secondLabel.textColor = [UIColor blackColor];

        [secondView addSubview:_secondLabel];
        
        [_secondLabel makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(minuteView.center);
        }];
        
        
        UIButton *button = UIButton.new;
        button.userInteractionEnabled = NO;
        button.layer.cornerRadius = 5;
        button.layer.borderColor = [UIColorFromRGB(0x36B85D) CGColor];
        button.layer.borderWidth = 1/[[UIScreen mainScreen] scale];
        //设置button中文字或图片的偏移量
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        [button setTitle:@"未开始" forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromRGB(0x36B85D) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        
        
        [notStartView addSubview:button];
        
        [button makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->notStartView.top).offset(0);
            make.right.equalTo(self->notStartView.right).offset(0);
            make.height.equalTo(24);
        }];
        
        
        number= UILabel.new;
        number.textColor = [UIColor grayColor];
        //number.text = @"200/件";
        number.font =[UIFont systemFontOfSize:14];
        //number.text = [NSString stringWithFormat:@"%d /件", activityModel.number];
        number.numberOfLines = 1;
        [notStartView addSubview:number];
        
        [number makeConstraints:^(MASConstraintMaker *make) {
            //make.top.equalTo(_notStartView.top).offset(0);
            make.right.equalTo(button.left).offset(-6);
            make.centerY.equalTo(self->notStartView.centerY);
            // make.height.equalTo(12);
        }];
        /********** 活动未开始*******/
        
        
        /**********活动已开始******/
        startingView = UIView.new;
        startingView.hidden = YES;
        [self addSubview:startingView];

        [startingView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(dottedLine.bottom).offset(8);
            make.left.equalTo(self.left).offset(8);
            make.right.equalTo(self.right).offset(-8);
            make.height.equalTo(24);
            
        }];


        UIView *startingTimeArea =UIView.new;
        startingTimeArea.layer.cornerRadius =12;
        startingTimeArea.backgroundColor = UIColorFromRGB(0xE65650);
        [startingView addSubview:startingTimeArea];


        UIImageView* startingimageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_time"]];
        [startingTimeArea addSubview:startingimageView];

        [startingimageView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(startingTimeArea.left).offset(8);
            make.centerY.equalTo(startingTimeArea.centerY);
            make.width.equalTo(11);
            make.height.equalTo(11);
        }];


        startingTimeLabel = UILabel.new;
        startingTimeLabel.textColor = [UIColor whiteColor];
        startingTimeLabel.font = [UIFont systemFontOfSize:14];
        startingTimeLabel.numberOfLines = 1;
        [startingTimeArea addSubview:startingTimeLabel];

        [startingTimeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(startingimageView.right).offset(3);
            make.centerY.equalTo(startingTimeArea.centerY);
        }];

        [startingTimeArea makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->startingView).offset(0);
            make.left.equalTo(self->startingView.left).offset(0);
            make.right.equalTo(self->startingTimeLabel.right).offset(8);
            make.height.equalTo(24);
        }];
        

        UIButton *startingButton = UIButton.new;
        startingButton.userInteractionEnabled = NO;
        startingButton.layer.cornerRadius = 5;
        startingButton.backgroundColor = UIColorFromRGB(0xE6564F);
        startingButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        [startingButton setTitle:@"立刻抢购" forState:UIControlStateNormal];
        [startingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        startingButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self->startingView addSubview:startingButton];

        [startingButton makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->startingView.top).offset(0);
            make.right.equalTo(self->startingView.right).offset(0);
            make.height.equalTo(24);
        }];


        self->startingGoodsNumberLabel = [UILabel new];
        self->startingGoodsNumberLabel.font = [UIFont systemFontOfSize:14];
        self->startingGoodsNumberLabel.textColor =[UIColor grayColor];
        self->startingGoodsNumberLabel.numberOfLines = 1;
        [self->startingView addSubview:self->startingGoodsNumberLabel];

        [self->startingGoodsNumberLabel makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(startingButton.centerY);
            make.right.equalTo(startingButton.left).offset(-10);
        }];
        
        
        /******已开始******/
        
        
        /******已结束*******/
        self->endedView = UIView.new;
        self->endedView.hidden = YES;
        [self addSubview:self->endedView];
        
        [self->endedView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(dottedLine.bottom).offset(8);
            make.left.equalTo(self.left).offset(8);
            make.right.equalTo(self.right).offset(-8);
            make.height.equalTo(24);
        }];
        
        
        UIImageView *endedTimeIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_time_black"]];
        [self->endedView addSubview:endedTimeIcon];
        
        [endedTimeIcon makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self->endedView.left).offset(8);
            make.centerY.equalTo(self->endedView.centerY);
            make.width.equalTo(11);
            make.height.equalTo(11);
        }];
        
        self->endedViewTimeLabel = [UILabel new];
        self->endedViewTimeLabel.font = [UIFont systemFontOfSize:14];
        self->endedViewTimeLabel.textColor = [UIColor blackColor];
        self->endedViewTimeLabel.numberOfLines = 1;
        [self->endedView addSubview:self->endedViewTimeLabel];
        
        [self->endedViewTimeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(endedTimeIcon.right).offset(3);
            make.centerY.equalTo(self->endedView.centerY);
        }];
        
        
        UIButton *endedButton = UIButton.new;
        endedButton.userInteractionEnabled = NO;
        endedButton.layer.cornerRadius = 5;
        endedButton.backgroundColor = UIColorFromRGB(0x999999);
        endedButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        [endedButton setTitle:@"已抢完" forState:UIControlStateNormal];
        [endedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        endedButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self->endedView addSubview:endedButton];
        
        [endedButton makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->endedView.top).offset(0);
            make.right.equalTo(self->endedView.right).offset(0);
            make.height.equalTo(24);
        }];
        
        self->endedGoodsNumberLabel = [UILabel new];
        self->endedGoodsNumberLabel.font = [UIFont systemFontOfSize:14];
        self->endedGoodsNumberLabel.textColor = [UIColor grayColor];
        self->endedGoodsNumberLabel.numberOfLines = 1;
        [self->endedView addSubview:self->endedGoodsNumberLabel];
        
        [self->endedGoodsNumberLabel makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(endedButton.centerY);
            make.right.equalTo(endedButton.left).offset(-10);
        }];
        
        
        self->numberLabel = [UILabel new];
        self->numberLabel.font = [UIFont systemFontOfSize:14];
        self->numberLabel.textColor = [UIColor grayColor];
        self->numberLabel.numberOfLines = 1;
        [self->endedView addSubview:self->numberLabel];
        
        [self->numberLabel makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(endedButton.centerY);
            make.left.equalTo(self->endedViewTimeLabel.right).offset(15);
            //make.right.equalTo(endedGoodsNumberLabel.left).offset(-5);
        }];
        
        /*********已结束********/
        
        
        
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
